package com.laysonx.data.police.util;

import com.laysonx.data.police.core.*;
import com.laysonx.data.police.support.DefaultBeanDisposeHandler;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.io.ResourceLoader;

import java.util.HashMap;
import java.util.Map;

/**
 * BeanDefinition工具
 * @author: Laysonx
 * @date: 2019/10/17 16:46
 */
public class BeanDefinitionUtil {

    /**
     * 初始化空 advisor 所需参数
     * 装配过程中会替换 advice
     * @author: Laysonx
     * @date: 2019/10/11 13:16
     * @param pointcut
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     */
    public static Map<String, Object> initAdvisorProperty(String pointcut) {
        Advice nullAdvice = (MethodInterceptor) invocation -> {
            Object obj = invocation.proceed();
            return obj;
        };

        Map<String,Object> property = new HashMap<>(2);
        property.put("expression", pointcut);
        property.put("advice", nullAdvice);
        return property;
    }

    /**
     * 扫描handler
     * @author: Laysonx
     * @date: 2019/10/17 16:54
     * @param registry
     * @param resourceLoader
     * @param scanHelperPackages
     * @return: void
     */
    public static void scanDataHelper(BeanDefinitionRegistry registry,ResourceLoader resourceLoader,String[] scanHelperPackages){

        HelperBeanDefinitionScanner scanner = new HelperBeanDefinitionScanner(registry);
        scanner.setResourceLoader(resourceLoader);
        scanner.registerFilters();
        scanner.doScan(scanHelperPackages);
    }

    /**
     * 注册初始化所需所有BeanDefinition
     * @author: Laysonx
     * @date: 2019/10/17 16:58
     * @param registry
     * @param pointcut
     * @return: void
     */
    public static void initBean(BeanDefinitionRegistry registry,String pointcut){
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, DataVerifyInfoConfigurer.class.getName(), DataVerifyInfoConfigurer.class);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, DataVerifyEntranceConfigurer.class.getName(), DataVerifyEntranceConfigurer.class);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, DataVerifyInfoChecker.class.getName(), DataVerifyInfoChecker.class);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, DataVerifyInfoConfigurerProcessor.class.getName(), DataVerifyInfoConfigurerProcessor.class);
        Map<String, Object> property = BeanDefinitionUtil.initAdvisorProperty(pointcut);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, AspectJExpressionPointcutAdvisor.class.getName(), AspectJExpressionPointcutAdvisor.class,property);
        BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, DefaultBeanDisposeHandler.class.getName(), DefaultBeanDisposeHandler.class);
    }
}
